﻿using Loxodon.Framework.Binding;
using UnityEngine;
using UnityEngine.UI;

namespace Master.Example.LoxodonFramework
{
    public class View : MonoBehaviour
    {
        private Button mBtnAdd;

        private Button mBtnSub;

        private Text mNumber;

        private void Awake()
        {
            // 主动调用一次，确保开启绑定服务
            var container = App.Container;
            
            // View 与 ViewModel 关联
            this.BindingContext().DataContext = new ViewModel();
        }

        void Start()
        {
            mBtnAdd = transform.Find("BtnAdd").GetComponent<Button>();
            mBtnSub = transform.Find("BtnSub").GetComponent<Button>();
            mNumber = transform.Find("Number").GetComponent<Text>();
            
            // 数据绑定
            var bindingSet = this.CreateBindingSet<View, ViewModel>();
            
            // vm.Count 通知 mNumber 更新视图
            bindingSet.Bind(mNumber).For(v => v.text).To(vm => vm.Count).OneWay();
            
            bindingSet.Bind(mBtnAdd).For(v => v.onClick).To(vm => vm.IncreaseCount);
            bindingSet.Bind(mBtnSub).For(v => v.onClick).To(vm => vm.DecreaseCount);
            
            bindingSet.Build();
        }
    }
}